package com.example.teamtribe.main.mine.setting.certify;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.common.model.UserModel;
import com.example.teamtribe.database.User;
import com.example.teamtribe.main.mine.setting.certify.CertifyModel.CertifyBack;
import com.example.teamtribe.utils.Utility;

public class CertifyPresenter implements CertifyContract.Presenter {

    private CertifyContract.View view;
    private CertifyModel model;

    public CertifyPresenter(CertifyContract.View view) {
        this.view = view;
        model = new CertifyModel();
    }

    @Override
    public void getCodeImage(int schoolId) {
        view.showProgress();
        model.getCodeImage(Utility.getToken(), schoolId, new CertifyBack() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.hideProgress();
                view.setCodeImage(bundle.getByteArray("image"), bundle.getString("sessionId"));
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }

            @Override
            public void error(String meg) {
                view.hideProgress();
                view.setView(meg);
            }
        });
    }

    @Override
    public void sendIdentify(int schoolId, String no, String psw, String code, String sessionId) {
        if (TextUtils.isEmpty(no) || TextUtils.isEmpty(psw) || TextUtils.isEmpty(code)) {
            view.showToast("请填写完整的验证信息");
            return;
        }

        view.showProgress();
        model.sendIdentify(Utility.getToken(), schoolId, no, psw, code, sessionId, new CertifyBack() {
            @Override
            public void success(@Nullable Bundle bundle) {
                final String meg = bundle.getString("ok");
                UserModel.getInstance().getUser(Utility.getToken(), Utility.getUser().getRealId(), new FileModel.FileBack() {
                    @Override
                    public void loadSuccess(String fileName) {

                    }

                    @Override
                    public void success(@Nullable Bundle bundle) {
                        Utility.setUser(((User) bundle.getParcelable("user")));
                        view.setResult();
                        view.hideProgress();
                        view.setView(meg);
                    }

                    @Override
                    public void failure(String meg) {
                        view.showToast(meg);
                    }
                });
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }

            @Override
            public void error(String meg) {
                view.hideProgress();
                view.setView(meg);
            }
        });
    }
}
